<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="团队" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="团队" clearable>
          <el-option
            v-for="dict in teamList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="救援人员类型" prop="userTypeId">
        <el-select v-model="queryParams.userTypeId" placeholder="救援人员类型" clearable>
          <el-option
            v-for="dict in dict.type.emg_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="userTypeId">
        <el-select v-model="queryParams.majorId" placeholder="专业" clearable>
          <el-option
            v-for="dict in dict.type.emg_user_major"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="" align="center" prop="id" v-if="true"/>
      <el-table-column label="队伍" align="center" prop="teamId" width="150">
        <template slot-scope="scope">
          <el-tag> {{ getDictTeamData(scope.row.teamId) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="救援人员类型" align="center" prop="userTypeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_user_type" :value="scope.row.userTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="account"/>
      <el-table-column label="密码" align="center" prop="password"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="专业字典" align="center" prop="majorId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_user_major" :value="scope.row.majorId"/>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatarUrl" width="200">
        <template slot-scope="scope">
          <ImagePreview v-if="scope.row.avatarUrl"
                        :height=100
                        :src="scope.row.avatarUrl"
                        :preview-src-list="[scope.row.avatarUrl]"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone"/>
      <el-table-column label="坐标" align="center" prop="lat"/>
      <el-table-column label="坐标" align="center" prop="lng"/>
      <el-table-column label="所属区域字典" align="center" prop="areaId"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="擅长" align="center" prop="goodAt"/>
      <el-table-column label="作战次数" align="center" prop="combatCount"/>
      <el-table-column label="描述备注信息" align="center" prop="remark"/>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改救援人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="团队" prop="teamId">
          <el-select v-model="form.teamId" placeholder="团队" clearable>
            <el-option
              v-for="dict in teamList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="救援人员类型" prop="userTypeId">
          <el-select v-model="form.userTypeId" placeholder="救援人员类型" clearable>
            <el-option
              v-for="dict in dict.type.emg_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="头像" prop="avatarUrl">
          <singleImageUpload v-model="form.avatarUrl"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="form.age" @change="handleChange" :min="16" :max="99" label="年龄"></el-input-number>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="性别" clearable>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item label="专业" prop="majorId">
          <el-select v-model="form.majorId" placeholder="专业" clearable>
            <el-option
              v-for="dict in dict.type.emg_user_major"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="坐标" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="坐标" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="所属区域字典" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属区域字典"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="擅长" prop="goodAt">
          <el-input v-model="form.goodAt" placeholder="请输入擅长"/>
        </el-form-item>
        <el-form-item label="作战次数" prop="combatCount">
          <el-input-number v-model="form.combatCount" @change="handleChangeCombat" :min="0" :max="999"
                           label="作战次数"></el-input-number>
        </el-form-item>
        <el-form-item label="描述备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入描述备注信息"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser} from "@/api/emergency/user";
import {getCustomDicts} from "@/api/system/dict/data";

export default {
  name: "User",
  dicts: ['sys_normal_disable', 'emg_user_type', 'emg_user_major', 'sys_user_sex'],
  data() {
    return {
      teamList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 救援人员表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamId: undefined,
        userTypeId: undefined,
        account: undefined,
        password: undefined,
        name: undefined,
        avatarUrl: undefined,
        age: undefined,
        sex: undefined,
        phone: undefined,
        majorId: undefined,
        lat: undefined,
        lng: undefined,
        areaId: undefined,
        address: undefined,
        goodAt: undefined,
        combatCount: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        teamId: [
          {required: true, message: "团队不能为空", trigger: "blur"}
        ],
        userTypeId: [
          {required: true, message: "救援人员类型不能为空", trigger: "blur"}
        ],
        account: [
          {required: true, message: "账号不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        age: [
          {required: true, message: "年龄不能为空", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "性别不能为空", trigger: "change"}
        ],
        majorId: [
          {required: true, message: "专业字典不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getCustomDictList();
    this.getList();
  },
  methods: {
    handleChange(value) {
      this.form.age = value
    },
    handleChangeCombat(value) {
      this.form.combatCount = value
    },
    /** 查询救援人员列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCustomDictList() {
      this.loading = true;
      getCustomDicts("custom_team_list").then(response => {
        this.teamList = response.data;
        this.loading = false;
      });
    },
    getDictTeamData(e) {
      for (var i = 0; i < this.teamList.length; i++) {
        if (this.teamList[i].value == e) {
          return this.teamList[i].label;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        teamId: undefined,
        userTypeId: undefined,
        account: undefined,
        password: undefined,
        name: undefined,
        avatarUrl: undefined,
        age: 16,
        sex: undefined,
        phone: undefined,
        majorId: undefined,
        lat: undefined,
        lng: undefined,
        areaId: undefined,
        address: undefined,
        goodAt: undefined,
        combatCount: undefined,
        remark: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加救援人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改救援人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除救援人员编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delUser(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
